/**
 * 自定义库素材
 * */





//获取大标题+按钮
function getTitleBtn(){
    console.log(vid,vtitle);
    //大标题
    $('#bigTitle').html('库素材： <span id="title">'+vtitle+'</span>');
    //新增按钮
    var addBtn = '<button type="button" class="am-btn am-btn-default am-btn-success" onclick="window.location.href=\'/admin/stoLayerForm.html?view=2&vid=\''+vid+';"> <span class="am-icon-plus"></span> 新增 </button>';
    console.log(addBtn);
    $('#addBtn').html(addBtn);
}
//获取数据列表
function getIndex(page){
    var formData = {
        adminAccess:adminAccess,
        page:page,
        size:pageSize,
        vid:vid
    };
    $.ajax({
        type:'POST',
        url:domain+'/stock/layer/index',
        data:formData,
        success:function(res){
            if (res.code!=0) {
                getSelfPop(res.msg); return
            }
            getSelfPop('获取成功');
            //显示数据
            var list = res.data;
            var total = res.total;
            $("#pageTail")[0].value = total;
            var i, title, h = '';
            for (i=0;i<list.length;i++) {
                h += '<tr class="gradeX">';
                h += '<td>';
                //if (list[i].videoImg) {
                //    h += '<a href="'+list[i].videoLink+'" target="_bank">';
                //    h += '<img src="'+list[i].videoImg+'" class="tpl-table-line-img">';
                //    h += '</a>';
                //} else {
                //    h += '/';
                //}
                h += '</td>';
                h += '<td class="am-text-middle">XX</td>';
                //h += '<td class="am-text-middle">'+list[i].videoTypeName+'</td>';
                //h += '<td class="am-text-middle">'+list[i].zanTotal+'</td>';
                //h += '<td class="am-text-middle">'+list[i].collectTotal+'</td>';
                h += '<td class="am-text-middle">';
                //if (list[i].del) {
                //    h += '<span style="color:#FF00FF;">已删除</span>';
                //} else {
                //    h += '<span style="color:#00FFFF;">未删除</span>';
                //}
                h += '</td>';
                h += '<td class="am-text-middle">';
                h += '<div class="tpl-table-black-operation">';
                //编辑
                h += '<a href="/admin/stockForm.html?view=2&vid='+list[i].videoId+'"> <i class="am-icon-pencil"></i> 编辑 </a>';
                h += ' &nbsp; ';
                //删除/还原
                h += ' &nbsp; ';
                if (list[i].del==0) {
                    h += '<a href="javascript:;" onclick="setDel('+list[i].videoId+')" class="tpl-table-black-operation-del"> <i class="am-icon-trash"></i> 删除 </a>';
                } else {
                    h += '<a href="javascript:;" onclick="setRedo('+list[i].videoId+')" class="tpl-table-black-operation-del"> <i class="am-icon-trash"></i> 还原 </a>';
                }
                h += '</td>';
                h += '</tr>';
            }
            $("tbody").html(h);
            //分页处理
            setPage(total);
        },
        error:function(xhr,type,errorThrown){
//                console.log('网络错误');
            getSelfPop('网络错误');
        }
    });
}
//分页处理
function setPage(total){
    var pageTotal = Math.ceil(total / pageSize) - 1; //页面总数
    var pageHome, pageTail, pagePre, pageNext; //首页，尾页，上一页，下一页
    pageHome = 0; //首页
    if (page > 0) { //上一页
        pagePre = page * 1 - 1;
    } else {
        pagePre = page;
    }
    if (page < pageTotal) { //下一页
        pageNext = page * 1 + 1;
    } else {
        pageNext = page;
    }
    pageTail = pageTotal; //尾页
    pageCurr = page * 1 + 1; //当前页面
    var h = '';
    h += '<li>';
    h += '共计'+total+'条，';
    h += '共'+pageTotal+'页， ';
    h += '当前第'+pageCurr+'页， ';
    h += '&nbsp; &nbsp; </li>';
    if (page==0) {
        h += '<li class="am-abled">';
    } else {
        h += '<li class="am-active">';
    }
    h += '<a href="javascript:;" onclick="setVideoPage('+pageHome+')">首页</a>';
    h += '</li>';
    if (page==0) {
        h += '<li class="am-abled">';
    } else {
        h += '<li class="am-active">';
    }
    h += '<a href="javascript:;" onclick="setVideoPage('+pagePre+')">上一页</a>';
    h += '</li>';
    if (page==pageTail) {
        h += '<li class="am-abled">';
    } else {
        h += '<li class="am-active">';
    }
    h += '<a href="javascript:;" onclick="setVideoPage('+pageNext+')">下一页</a>';
    h += '</li>';
    if (page==pageTail) {
        h += '<li class="am-abled">';
    } else {
        h += '<li class="am-active">';
    }
    h += '<a href="javascript:;" onclick="setVideoPage('+pageTail+')">尾页</a>';
    h += '</li>';
    $("#pageLi").html(h);
}
//获取分页数据
function setVideoPage(next){
    var pageTail = $("#pageTail").val();
    if (next==Number(page) && Number(page)==0) {
        getSelfPop('已在首页'); return
    }
    else if (next==Number(page) && Number(page)==Number(pageTail)) {
        getSelfPop('已在尾页'); return
    }
    else if (next==Number(page)) {
        getSelfPop('已在当前页面'); return
    }
    window.location.href = domain+'/stock.html?view='+view+'&page='+next;
}
//设置删除
function setDel(vid){
    var formData = {
        adminAccess:adminAccess,
        vid:vid
    };
    $.ajax({
        type:'POST',
        url:domain+'/stock/del',
        data:formData,
        success:function(res){
            console.log(res);
            if (res.code!=0) {
//                    console.log(res.msg); return
                getSelfPop(res.msg); return
            }
            getSelfPop('删除成功');
            setTimeout(function(){
                window.location.href = '';
            }, 1000);
        },
        error:function(xhr,type,errorThrown){
            console.log('网络错误');
        }
    });
}
//设置还原
function setRedo(vid){
    var formData = {
        adminAccess:adminAccess,
        vid:vid
    };
    $.ajax({
        type:'POST',
        url:domain+'/stock/redo',
        data:formData,
        success:function(res){
            console.log(res);
            if (res.code!=0) {
//                    console.log(res.msg); return
                getSelfPop(res.msg); return
            }
            getSelfPop('删除成功');
            setTimeout(function(){
                window.location.href = '';
            }, 1000);
        },
        error:function(xhr,type,errorThrown){
            console.log('网络错误');
        }
    });
}